<?php
/* INDEX.PHP $ 2009/12/23 11:23:18
*/    
@ob_start("ob_gzhandler");

$ua = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('{docomo|up\.browser|j\-phone|vodafone|softbank/|pdxgw|ddipocket|willcom|astel|l\-mode}i', $ua)){
 $loc = 'Location: http://'.str_replace(array('\/','//'),'/',$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']).'/moby.php');
 $uid = htmlspecialchars($_GET['UID'], ENT_QUOTES);
 if(is_numeric(str_replace('d', '', $uid))){
   header($loc.'?mode=show&UID='.$uid); exit;
 } else if($_GET['mode'] == ''){
  header($loc); exit;
 }
}

include_once('owner/ini.inc.php');
include_once('utils.php');
include_once('session.class.php');

BenchMark(); // ベンチスタート:)

define('ADMIN', Auth() ? TRUE : FALSE);

if(GET_STATS==TRUE) include_once(PATH.'stat/log.php');

include_once('cache.php');

header ("Content-Type: text/html; charset=".ENCODE);

mb_language('Japanese');
mb_http_output(ENCODE);
mb_internal_encoding(ENCODE);

/* ------[ リンクバー ブロック ]------ */
$tag_icon = ROOT_PATH.$theme_dir.'Images/tag.png';
$category_icon = ROOT_PATH.$theme_dir.'Images/category.png';
$archives_icon = ROOT_PATH.$theme_dir.'Images/archives.png';
$refresh_icon = ROOT_PATH.$theme_dir.'Images/refresh.png';
$search_icon = ROOT_PATH.$theme_dir.'Images/search.png';
$gallery_icon = ROOT_PATH.$theme_dir.'Images/gallery.png';
$login_icon = ROOT_PATH.$theme_dir.'Images/login.png';
$control_icon = ROOT_PATH.$theme_dir.'Images/control.png';

$link_pre = MODE_STATIC ? '' : 'index.php?mode='; $apres = MODE_STATIC ? '/' : '';
$linkbar = '
<div id="linkbar">
 <ul>
  <li class="tags"><a href="'.$link_pre.'tags'.$apres.'"><img src="'.$tag_icon.'" alt="Tags" title="タグ別" /></a></li>
  <li class="categories"><a href="'.$link_pre.'category'.$apres.'"><img src="'.$category_icon.'" alt="Category" title="カテゴリー別" /></a></li>
  <li class="archives"><a href="'.$link_pre.'archives'.$apres.'"><img src="'.$archives_icon.'" alt="Archives" title="アーカイブ別" /></a></li>
  <li class="top"><a href="'.ROOT_PATH.'"><img src="'.$refresh_icon.'" alt="Main" '.($UID!=''?'title="トップページへ"':'title=" 更 新 "').' /></a></li>
  <li class="search"><a href="'.$link_pre.'search'.$apres.'"><img src="'.$search_icon.'" alt="Search" title="検索" /></a></li>
  <li><a href="'.$link_pre.'gallery'.$apres.'"><img src="'.$gallery_icon.'" alt="Gallery" title="画像ギャラリー" /></a></li>
  '.(ADMIN==TRUE?'<li class="control"><a href="admin.php"><img src="'.$control_icon.'" alt="Control" title="管理モード" /></a></li>'.NL:'')
   .(ADMIN==TRUE?'  <li class="logout"><a href="admin.php?mode=logout"><img src="'.$login_icon.'" alt="Logoff" title="ログアウト" /></a></li>':
               '  <li class="login"><a href="#weblog"><img id="login-entree" src="'.$login_icon.'" alt="Login" title="ログイン" /></a></li>').'
 </ul>
</div>
';
/* ------[ #リンクバー ブロック ]------ */

/* ------[ ブロッグバー ブロック ]------ */
if(USE_BLOG_BAR){
 if(is_array($img=my_glob('blogBar', $theme_dir)) && !empty($img)){
  $size = getimagesize($img[0]);
  $on = 'Generated on '.date('F j, Y H:i:s', substr($img[0],-14,10));
  if(ADMIN==TRUE){
   $blogbar = '
   <a href="admin.php?mode=blogbar"><img src="'.$img[0].'" '.$size[3].' alt="blogBar" title="BLOG BARの編集" /></a>
   ';
  } else {
   $blogbar = '
   <img src="'.$img[0].'" '.$size[3].' alt="blogBar" title="'.$on.'" />
   ';
  }
 } else $blogbar = '';
} else $blogbar = '';
/* ------[ #ブロッグバー ブロック ]------ */

/* ------[ エントリー ブロック ]------ */
$recently = NL;
if(isset($_cache['recently']) && is_array($_cache['recently'])){
 foreach($_cache['recently'] as $id=>$title){
  if(strpos($id, 'd')) continue;
  list($title, $pm_link) = explode(',', $title);
  $_title = my_substr($title, 0, 20);
  $recently .= '     <li>'
  . '<a href="'.(MODE_STATIC ? $pm_link : $_self.'?UID='.$id).'" title="'.$title.' '.date('Y/m/d', $id).'">'.$_title.'</a></li>'.NL;
 }
} else $EMPTY['recently'] = TRUE;
/* ------[ #エントリー ブロック ]------ */

/* ------[ 作成ページ ブロック ]------ */
$_pages = NL;
if(isset($_cache['pages']) && is_array($_cache['pages'])){
 foreach ($_cache['pages'] as $dat=>$pagename){
  list($pagename, $fname, $ontop) = explode('|', $pagename);
  if($ontop > 0){
   include_once('modules/pages.inc.php');
   $f = PAGE_DIR.$fname.'.txt';
   if($ontop==1){ # `ontop`
    if(empty($_VARS) && is_file($f)) $DIVISION['header'] .= outputPage(get_file_content($f), $fname);
   }
   if($ontop==2){ # `modal`
    if(is_file($f)) $DIVISION['header'] .= outputPage(get_file_content($f), $fname);
    continue;
   }
  }
  $link = MODE_STATIC ? ROOT_PATH.'pages/'.$fname.'/' : $_self.'?mode=pages&amp;aim='.$fname;
  $_pages .= '     <li><a title="最終更新日：'.date('Y/m/d H:i:s', $dat).'" href="'.$link.'">'.$pagename.'</a></li>'.NL;
 }
}
/* ------[ #作成ページ ブロック ]------ */

/* ------[ コメント ブロック ]------ */
$comments = NL;
if(isset($_cache['recent_comments']) && is_array($_cache['recent_comments'])){
 foreach($_cache['recent_comments'] as $c){
  $_cmts = explode("\t", $c);
  list($uid, $title) = explode('|', $_cmts[0]);
  if(strpos($uid, 'd')) continue;
  $_title = my_substr($title, 0, 15);
  $pm_link = MODE_STATIC ? $uid : 'index.php?UID='.$uid;
  $comments .= "     <li><a title=\"$title ".date('Y/m/d',$uid)."\" href=\"$pm_link#responses\">$_title</a>\n";
  $comments .= '      <ul>'.NL;
  foreach ($_cmts as $i=>$_c){
   if($i==0) continue;
   list($t,$p) = explode('|', $_c);
   $dat = date('m/d H:iA', $t);
   $new = (time() - $t < NEW_SPAN * 3600) ? ' <img src="'.$theme_dir.'Images/new.png" class="new-entry" alt="" title="'.NEW_SPAN.'時間以内のコメント" />' : '';
   if(strstr($p, "'")){
    $aw = '      <li class="nest2" title="'.$dat.'">';
    $p = str_replace("'", '', $p);
   } else $aw = '       <li class="nest1" title="'.$dat.'">';
   if($p=='$$'){
    $aw = '      <li class="show-all-comments" title="すべてのコメントを表示します">';
    $p = '最新'.(ENTRY_BOX * 2).'件のコメント';
   } else $_col = '';
   $comments .=  "$aw <a href=\"$pm_link#CID$t\">$p</a>$new</li>\n";
  }
  $comments .= '      </ul>'.NL.'     </li>'.NL;
 }
}

/* ------[ #コメント ブロック ]------ */

/* ------[ トラックバック ブロック ]------ */
$trackbacks = NL;
$recent_trackback = !empty($_cache['recent_trackbacks']) ? $_cache['recent_trackbacks'] : '';

if(!empty($recent_trackback)){
 foreach ($recent_trackback as $tid=>$tb){
  if(strpos($tid, 'd')) continue;
  $tbs = explode('|', $tb[0]);
  $title = $tbs[1];   $ftitle = isset($tbs[2]) ? $tbs[2] : $tbs[1];
  if(strpos($ftitle, ',')) list($ftitle, $pm_title) = explode(',', $ftitle);
  $pm_link = MODE_STATIC ? $pm_title : 'index.php?mode=trackback&amp;UID='.$tid;
  $trackbacks .=  '     <li><a href="'.$pm_link.'#trackback" title="'.$ftitle.'">'.$title.'</a>'.NL;
  for($i = 1; $i < count($tb); $i++){
   $trackbacks .= '      <ul>'.NL;
   list($entry,$blog_name) = explode('|',$tb[$i]);
   $new = (time()-$entry < NEW_SPAN*60*60) ? ' <img src="'.$theme_dir.'Images/new.png" class="new-entry" alt="" title="'.NEW_SPAN.'時間以内のトラックバック" />' : '';
   $trackbacks .= '       <li class="nest1"><a href="'.$pm_link.'#TID'.$entry.'" title="Tracked on '.date('Y/m/d H:iA', $entry).'">'.$blog_name.'</a>'.$new.'</li>'.NL;
   $trackbacks .= '      </ul>'.NL;
  }
  $trackbacks .= '     </li>'.NL;
 }
} else $trackbacks .= '<li class="no-ping" title="今のところトラックバックはありません">no trackback</li>'.NL;
/* ------[ #トラックバック ブロック ]------ */

/* ------[ カテゴリー ブロック ]------ */
$categories = NL;
if(!empty($_cache['category'])){
 foreach ($_cache['category'] as $category=>$num_articles){
  if(strpos($category, "\t")) list($category, $catlink) = explode("\t", $category);
  if(strpos($category, ',')){
   list($main, $sub) = explode(',', $category);
  } else {
   $main = $category; $sub = '';
  }
  list($nominal, $real) = explode('|', $num_articles);
  $disp_num = ADMIN ? $real : $nominal;
  $_cat = ($sub=='') ? $main : $sub;
  $categories .= '     <li class="'.($sub==''?'main-category':'sub-category').'">'.
  ($disp_num==0?'<span>'.$_cat.' [ 0 ]</span>' :
  '<a href="'.(MODE_STATIC ? 'category/' : 'index.php?mode=category&amp;aim=').$catlink.(MODE_STATIC ? '/' : '').'">'.$_cat.' [ '.$disp_num.' ] </a>').'</li>'.NL;
 }
}
/* ------[ カテゴリー ブロック ]------ */

/* ------[ アーカイブ ブロック ]------ */
$archives = NL;
if(!empty($_cache['articles_volume'])){
 $i = 0;
 foreach($_cache['articles_volume'] as $dat=>$count){
  if($i==ARCHIVES_ENTRY){
   $archives .= '     <li><a href="'.(MODE_STATIC ? ROOT_PATH : $_self.'?mode=').'archives'.(MODE_STATIC ? '/' : '').'" title="すべてのアーカイブを表示">all</a></li>
  ';
   break;
  }
  if($count==0) continue;
  $disp_num = ADMIN ? $count[0] : $count[1];
  $year = substr($dat, 0, 4); $month = substr($dat, 4, 2);
  $_log = $year.'年'.$month.'月 ['.$disp_num.']';
  $archives .= '     <li><a href="'.(MODE_STATIC ? $year.'/'.$month.'/'.$displayMode : $_self.'?date='.$dat.'&amp;view='.$displayMode).'">'.$_log.'</a></li>'.NL;
  $i++;
 }
}
/* ------[ #アーカイブ ブロック ]------ */

/* ------[ その他 ブロック ]------ */
$others = NL;
$rss20 = (RSS2) ? '     <li><a href="feeds/rss2.0.xml"><img src="Images/rss20.png" alt="RSS2.0" class="micro-banner" /></a></li>' : '';
$atom10 = (ATOM10) ? '     <li><a href="feeds/atom1.0.xml"><img src="Images/atom10.png" alt="atom1.0" class="micro-banner" /></a></li>' : '';
$xhtml10 = '     <li><a href="http://validator.w3.org/check/referer"><img src="Images/xhtml10.png" alt="valid XHTML1.0" class="micro-banner" /></a></li>';
$css2 = '     <li><a href="http://jigsaw.w3.org/css-validator/"><img src="Images/w3c-css.png" alt="valid CSS2" class="micro-banner" /></a></li>';
$copyright = '     <li><a href="http://p2b.jp"><img src="Images/ppblog180.png" alt="Credit" class="micro-banner" title="ppBlog'.PPBLOG_VERSION.' powered" /></a></li>
';
$others = $rss20.NL.$atom10.NL.$xhtml10.NL.$css2.NL.$copyright.NL;
/* ------[ #その他 表示ブロック ]------ */

/* ------[ タグ ブロック ]------ */
$tags = NL;
if(!empty($_cache['tags'])){
 $i = 0;
 foreach ($_cache['tags'] as $tag=>$size){
  list($nominal, $real) = explode('|', $size[0]);
  $size[0] = ADMIN ? $real : $nominal;
  if($size[0]==0) continue;
  if($i >= TAGS_ENTRY){
   $tags .= '     <li class="tags-end"><a href="'.(MODE_STATIC ? 'tags/' : 'index.php?mode=tags').'" title="すべてのタグエントリーを表示">all</a></li>';
   break;
  }
  $i++;
  $link = MODE_STATIC ? 'tags/'.urlencode(urlencode($tag)).'/' : 'index.php?mode=tags&amp;aim='.urlencode(urlencode($tag));
  $tags .= '     <li title="'.($size[0]==1?'1 entry':$size[0].' entries').'"><a href="'.$link.'" rel="tag" style="font-size:'.$size[1].'px;">'.$tag.'</a></li>'.NL;
 }
 $tags .= NL;
} else $tags = '<li class="no-tag" title="タグは使われていません">no tag used</li>'.NL;

/* ------[ #タグ ブロック ]------ */

switch ($mode){
 case 'login' : case 'logout' : _header('admin.php'); break;
 case 'category' : show_category(); break;
 case 'gallery' : include_once('modules/gallery.inc.php'); break;
 case 'archives' : show_archives(); break;
 case 'trackback' : include_once('modules/trackback.inc.php'); break;
 case 'download' : include_once('modules/download.inc.php'); break;
 case 'search' : include_once('modules/search.inc.php'); do_search(p_('words')); break;
 case 'comment' : include_once('modules/comment.inc.php'); catch_comment($mode); break;
 case 'write' : include_once('modules/write.inc.php'); write_form(); break;
 case 'edit' : include_once('modules/edit.inc.php'); edit_form($UID); break;
 case 'del' : include_once('modules/delete.inc.php'); del_form($UID); break;
 case 'pages' : include_once('modules/pages.inc.php'); break;
 case 'tags' : include_once('modules/tags.inc.php'); break;
 case 'mobile' : include_once('modules/mobile.inc.php'); break;
 default :
  if($UID != ''){
   show_box($UID, TRUE, '', TRUE);
  } elseif ($displayMode=='list'){
   show_lists();
  } else {
   HEADLINE_MODE ? show_headline() : show_box_all();
  }
  break;
}

include_once('holiday.class.php');        // 祝日クラスの呼び込み
include_once('modules/calendar.inc.php'); // $LINESの後でカレンダーを呼び出す

$DIVISION['title'] = ($UID && $DIVISION['title'] != '') ? $DIVISION['title'] : BLOG_NAME;
$DIVISION['loginfo'] = (ADMIN) ? '<span>'.$SESSION->owner().'</span> としてログイン中' : '';
$DIVISION['linkbar'] = $linkbar;
$DIVISION['search'] = isset($_SEARCH_FORM) ? $_SEARCH_FORM : '';
$DIVISION['recently'] = $recently;
$DIVISION['tags'] = $tags;
$DIVISION['pages'] = $_pages;
$DIVISION['comments'] = $comments;
$DIVISION['trackbacks'] = $trackbacks;
$DIVISION['categories'] = $categories;
$DIVISION['archives'] = $archives;
$DIVISION['others'] = $others;

echoHTML(/*array('photoeffect.min.js', 'oParts.min.js', 'lib.min.js')*/); // これで HTMLを出力
exit;
?>
